//Copyright 2012 - Jonathan Allen

#if WINDOWS_PHONE
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Phone.Controls;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Phone.Tasks;
#endif

namespace Granite.Xaml.Internals
{
#if WINDOWS_PHONE
	/// <summary>
	/// This INavigator is used for Windows Phone applications
	/// </summary>
	[CLSCompliant(false)]
	public partial class PhoneApplicationFrameNavigator : INavigator
	{
		readonly PhoneApplicationFrame m_Frame;

		/// <summary>
		/// Creates a new PhoneApplicationFrameNavigator
		/// </summary>
		/// <param name="frame"></param>
		public PhoneApplicationFrameNavigator(PhoneApplicationFrame frame)
		{
			if (frame == null)
				throw new ArgumentNullException("frame", "frame is null.");
			Contract.EndContractBlock();

			m_Frame = frame;
		}

		/// <summary>
		/// Navigate to the indicated URI
		/// </summary>
		/// <param name="target"></param>
		public void Navigate(Uri target)
		{
			PhoneApplicationFrame.Navigate(target);
		}

		/// <summary>
		/// Indicates that GoBack can be safely called.
		/// </summary>
		public bool CanGoBack
		{
			get { return PhoneApplicationFrame.CanGoBack; }
		}

		/// <summary>
		/// Indicates that GoForward can be safely called.
		/// </summary>
		public bool CanGoForward
		{
			get { return PhoneApplicationFrame.CanGoForward; }
		}

		/// <summary>
		/// Return to the previous View in the navigation journal.
		/// </summary>
		public void GoBack()
		{
			PhoneApplicationFrame.GoBack();
		}

		/// <summary>
		/// Go to the next view in the navigation journal.
		/// </summary>
		public void GoForward()
		{
			PhoneApplicationFrame.GoForward();
		}

		/// <summary>
		/// Access the underlying Frame for this Navigator
		/// </summary>
		public PhoneApplicationFrame PhoneApplicationFrame
		{
			get 
			{
				Contract.Ensures(Contract.Result<PhoneApplicationFrame>() != null);
				return m_Frame; 
			}
		}

		[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		[ContractInvariantMethod]
		void ContractInvariant()
		{
			Contract.Invariant(m_Frame != null);
		}



		/// <summary>
		/// Indicates whether or not websites are included in the journal
		/// </summary>
		public bool IncludeWebInNavigation
		{
			get { return false; }
		}
	}
#endif

#if WINDOWS_PHONE && WP70 && !WP71

	partial class PhoneApplicationFrameNavigator {
		/// <summary>
		/// Opens a website.
		/// </summary>
		/// <param name="target"></param>
		public void OpenWebsite(Uri target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			Contract.EndContractBlock();

			Contract.Assume(!string.IsNullOrEmpty(target.ToString()));

			WebBrowserTask wbt = new WebBrowserTask();
			wbt.URL = target.ToString();
			wbt.Show();

		}
	}
#endif
#if WINDOWS_PHONE && WP71

	partial class PhoneApplicationFrameNavigator {
		/// <summary>
		/// Opens a website.
		/// </summary>
		/// <param name="target"></param>
		public void OpenWebsite(Uri target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			Contract.EndContractBlock();

			Contract.Assume(!string.IsNullOrEmpty(target.ToString()));

			WebBrowserTask wbt = new WebBrowserTask();
			wbt.Uri = target;
			wbt.Show();
		}
	}
#endif
}